package com.kuaimacode.kframework.agent.services.weather;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuaimacode.kframework.agent.util.SessionUtil;
import com.kuaimacode.kframework.common.Constant;
import com.kuaimacode.kframework.mybatis.mapper.weather.WeatherCfgMapper;
import com.kuaimacode.kframework.mybatis.models.sys.Dict;
import com.kuaimacode.kframework.mybatis.models.weather.WeatherCfg;
import com.kuaimacode.kframework.service.DictService;
import com.kuaimacode.kframework.util.DictUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * @author anzy
 * @date 2019/04/07
 */
@Service
public class WeatherCfgService extends ServiceImpl<WeatherCfgMapper, WeatherCfg> {

		@Autowired
		private DictService dictService;

    	/**
    	 *  分页查找所有的数据
    	 * @param pageNum 当前页码，从1开始
    	 * @param pageSize  一页显示多少行
    	 * @return
    	 */
    	public PageInfo<WeatherCfg> findAll(int pageNum, int pageSize,WeatherCfg weatherCfg) {
    		PageHelper.startPage(pageNum, pageSize);
			QueryWrapper queryWrapper = new QueryWrapper();
			queryWrapper.eq("agent_id",SessionUtil.getAgentId());
			Optional.ofNullable(weatherCfg.getWeatherName()).ifPresent(weatherName->queryWrapper.like("weather_name",weatherName));

    		List<WeatherCfg> datas = baseMapper.selectList(queryWrapper);
    		return new PageInfo<WeatherCfg>(datas);
    	}

    	@Transactional
    	public void init(){
			//先删除分站配置
			QueryWrapper queryWrapper = new QueryWrapper();
			queryWrapper.eq("agent_id",SessionUtil.getAgentId());
			baseMapper.delete(queryWrapper);
    		//复制平台配置
			baseMapper.init(SessionUtil.getAgentId(),SessionUtil.getUserId());

		}


		@Transactional
    	public String saveWeatherCfg(WeatherCfg weatherCfg){
			try {
				//判断是否已经存在
				QueryWrapper queryWrapper = new QueryWrapper();
				queryWrapper.eq("weather_id",weatherCfg.getWeatherId());
				queryWrapper.eq("agent_id",SessionUtil.getAgentId());
				WeatherCfg weather = baseMapper.selectOne(queryWrapper);
				if(weather!=null){
					//更新
					weather.setUpdateAt(new Date());
					weather.setUpdateBy(SessionUtil.getUserId());
					weather.setAmt(weatherCfg.getAmt());
					baseMapper.updateById(weather);
				}else{
					//insert
					weatherCfg.setWeatherName(DictUtil.keyValue("WEATHER",weatherCfg.getWeatherId()+""));
					weatherCfg.setWeatherId(weatherCfg.getWeatherId());
					weatherCfg.setAgentId(SessionUtil.getAgentId());
					weatherCfg.setCreateAt(new Date());
					weatherCfg.setCreateBy(SessionUtil.getUserId());
					baseMapper.insert(weatherCfg);
				}
				return "success";
			}catch (Exception e){
				e.printStackTrace();
				return e.getMessage();
			}

		}




	/**
	 * 查询分站特殊天气配置
	 * @param agentId
	 * @return
	 */
	public List<WeatherCfg> queryAgentWeatherCfg(String agentId){

    		return baseMapper.queryAgentWeatherCfg(agentId);

		}



}

